public class Solution814 {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public TreeNode pruneTree(TreeNode root) {
        TreeNode treeNode=new TreeNode(-1,root,null);
        dfs(treeNode,root);
        return treeNode.left;
    }

    private void dfs(TreeNode treeNode, TreeNode root) {
        if (root==null){
            return;
        }
        dfs(root,root.left);
        dfs(root,root.right);
        if (root.left==null && root.right==null && root.val==0){
            if (treeNode.left==root){
                treeNode.left=null;
            }else {
                treeNode.right=null;
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(new Solution814().pruneTree(new TreeNode(1,null,new TreeNode(0,new TreeNode(0),new TreeNode(1))).right.left));
    }
}
